package cn.mothin.frame.core.base;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.LogFactory;

import cn.mothin.frame.core.annotation.Dictionary;
import cn.mothin.frame.core.interfaces.IDictionary;

/**
 * @author zhoush
 *
 */
public abstract class BaseDictionary implements IDictionary {
	protected String id;
	protected int ordinal;
	protected String caption;
	protected String nameDisplay;
	protected String memo;
	protected String parentId;

	public BaseDictionary() {

	}

	public BaseDictionary(String id, String caption, int ordinal) {
		this.id = id;
		this.caption = caption;
		this.ordinal = ordinal;
	}

	public BaseDictionary(String id, String caption, int ordinal,
			String parentId) {
		this.id = id;
		this.caption = caption;
		this.ordinal = ordinal;
		this.parentId = parentId;
	}

	public BaseDictionary(String id, String caption, int ordinal, String memo,
			String parentId) {
		this.id = id;
		this.caption = caption;
		this.ordinal = ordinal;
		this.memo = memo;
		this.parentId = parentId;
	}

	public static List<IDictionary> getDictionaryList(Class<?> c) {
		List<IDictionary> list = new ArrayList<IDictionary>();
		for (Field field : c.getFields()) {
			if (field.getType() == c && Modifier.isStatic(field.getModifiers())
					&& Modifier.isPublic(field.getModifiers())) {
				try {
					list.add((IDictionary) field.get(null));
				} catch (Exception e) {
					LogFactory.getLog(c).error(e);
				}
			}
		}
		return list;
	}

	public String getDictionaryTypeId() {
		Class<?> c = this.getClass();
		if (c.isAnnotationPresent(Dictionary.class)) {
			Dictionary dictionary = c.getAnnotation(Dictionary.class);
			return dictionary.id();
		}
		return null;
	}

	public String getId() {
		return id;
	}

	public int getOrdinal() {
		return ordinal;
	}

	public String getCaption() {
		return caption;
	}

	public String getMemo() {
		return memo;
	}

	public String getParentId() {
		return parentId;
	}

	public String getNameDisplay() {
		return nameDisplay;
	}

}
